﻿using FirstFloor.ModernUI.Windows.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TanmiaGrp.Basis;

namespace TanmiaGrp.GeneralLedger.WinApp.Scenarios
{
    /// <summary>
    /// Interaction logic for JournalBrowserView.xaml
    /// </summary>
    public partial class JournalBrowserView : UserControl
    {
        public JournalBrowserView()
        {
            InitializeComponent();
            DataContext = new JournalBrowserViewModel() { Close = CloseView };
        }

        /// <summary>
        /// Called when Pop-out button is pressed, to popup the window from the docked workspace.
        /// </summary>
        private void OnPopout(object sender, RoutedEventArgs e)
        {
            // Create new (Editor | Browser)View and assign the same (Editor | Browser)ViewModel object to it.
            // Hide the popout button because no need to popout an already popout-ed (Editor | Browser)View.
            JournalBrowserView popoutBrowserView = new JournalBrowserView();
            popoutBrowserView.DataContext = this.DataContext;
            popoutBrowserView.windowBar.PopoutVisibility = System.Windows.Visibility.Collapsed;

            // Create and show the popuped window.
            FloatWindow popoutWindow = new FloatWindow()
            {
                Content = popoutBrowserView,
                ShowActivated = true,
                Owner = Application.Current.MainWindow
            };

            popoutWindow.Show();

            // Notify MainWindowViewModel to keep track for new popoutWindow.
            Events.FloatWindowShown.Fire(popoutWindow);
        }

        /// <summary>
        /// Close the View that contains current (Editor | Browser)View.
        /// Because one ViewModel object may control many (Editor | Browser)ViewModel objects, we have to determine where 
        /// currrent (Editor | Browser)View is contained to close it accordingly.
        /// </summary>
        /// <param name="sender">Represetnt the button that send Close view.</param>
        public void CloseView(object sender)
        {
            FrameworkElement senderElm = sender as FrameworkElement;

            // check if JournalEditorView is contained within FloatWindow
            FloatWindow floatWindow = senderElm.TryGetParent<FloatWindow>();
            if (floatWindow != null)
                floatWindow.Close();

            // otherwise if JournalEditorView is contained within ModernFrame that mean it is docked.
            ModernFrame modernFrame = senderElm.TryGetParent<ModernFrame>();
            if (modernFrame != null)
                modernFrame.Source = null;
        }

        private void UserControl_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            FrameworkElement senderElm = e.OriginalSource as FrameworkElement;
            DataGrid grid = senderElm.TryGetVisualParent<DataGrid>();
            ModernButton button = senderElm.TryGetVisualParent<ModernButton>();

            if (grid == null && button == null)
                dbAccounts.SelectedIndex = -1;
        }
    }
}
